<?php

class Request_Http extends Request
{

	/**
	 * @param string $path
	 * @return string
	 */
	static function _sanitizePath($path)
	{
		$path = str_replace('-', '', $path);

		if (empty($path))
		{
			$path = 'core.index';
		}

		$arr = explode('.', $path);

		$segment = array(
			'admin',
			'client',
			'manage',
			'browse',
			'ajax',
			'api',
			'profile',
		);

		if (!Agilet::hasModule($arr[0]))
		{
			if (in_array($arr[0], $segment))
			{
				if (Agilet::hasModule($arr[1]))
				{
					$t = $arr[0];
					$arr[0] = $arr[1];
					$arr[1] = $t;
				}
				else
				{
					array_unshift($arr, 'core');
				}
			}
			else
			{
				array_unshift($arr, 'core');
			}
		}
		return $path;
	}

	/**
	 * constructor.
	 */
	public function __construct()
	{

		$this -> _method = self::getServer('REQUEST_METHOD', 'GET');

		$path = isset($_REQUEST['-path']) ? $_REQUEST['-path'] : '/';
		$path = strtolower(trim(preg_replace("#(\/)+#", '.', $path), '.'));

		if (null === self::$_alias)
		{
			self::$_alias = Config::readFile('alias');
		}

		if (isset(self::$_alias[$path]))
		{
			$path = self::$_alias[$path];
		}

		$path = self::_sanitizePath($path);

		$arr = explode('.', $path);

		$this -> _path = $this -> _findPath($path);
		
		$this -> _class = Agilet::getCanonicalClass($this -> _path, 'action');
	}

	protected function _findPath($original_path)
	{
		$pos = strpos($original_path, '.');

		$path = substr($original_path, 0, $pos) . '.class.action.' . substr($original_path, $pos + 1);

		$path = str_replace('.', '/', $path);
		

		do
		{
			$len1 = strlen($path);
			
			if (file_exists($filename = SYSTEM_MODULE_PATH . '/' . $path . '.php'))
			{
				require_once $filename;
				break;
			}
			
			if (file_exists($filename = SYSTEM_MODULE_PATH . '/' . $path . '/index.php'))
			{
				$path .= '/index';
				require_once $filename;
				break;
			}
			
			$path = substr($path, 0, strrpos($path, '/'));
			$len2 = strlen($path);
		}
		while($len2 < $len1);

		if ($len1 == $len2)
		{
			require_once SYSTEM_MODULE_PATH . '/core/class/action/error/404.php';
			return 'core.error.404';
		}

		return str_replace('.class.action.', '.',str_replace('/', '.', $path));
	}

	/**
	 * Retrieve an array of parameters
	 *
	 * Retrieves a merged array of parameters, with precedence of userland
	 * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
	 * userland params will take precedence over all others).
	 *
	 * @return array
	 */
	public function getParams()
	{
		return $this -> _params + $_GET + $_POST;
	}

	/**
	 * Retrieve a parameter
	 *
	 * Retrieves a parameter from the instance. Priority is in the order of
	 * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
	 * parameter matching the $key is not found, null is returned.
	 *
	 * If the $key is an alias, the actual key aliased will be used.
	 *
	 * @param mixed $key
	 * @param mixed $default Default value to use if key not found
	 * @return mixed
	 */
	public function getParam($key, $value = null)
	{
		if (isset($this -> _params[$key]))
		{
			return $this -> _params[$key];
		}
		elseif (isset($_GET[$key]))
		{
			return $_GET[$key];
		}
		elseif (isset($_POST[$key]))
		{
			return $_POST[$key];
		}

		return $value;
	}

}
